package net.bitacademy.java23.web;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import net.bitacademy.java23.service.RouteService;
import net.bitacademy.java23.vo.Route;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("route/*")
public class RouteController {
	
	@Resource
	private ServletContext servletContext;
	
	private RouteService routeService;

	@Required
	@Resource
	public void setRouteService(RouteService routeService)
	{
		this.routeService = routeService;
	}
	
	
	
	/// list
	@RequestMapping("list")
	public String list(Model model) throws Exception
	{
		Collection<Route>list = routeService.listRoute();
		model.addAttribute("list", list);
		return "route/list";
	}
	
	/// listforajax
	@RequestMapping(value="list", produces="application/json")
	@ResponseBody
	public List<Route> listForAjax() throws Exception
	{
		return routeService.listRoute();
	}
	
	/// view
	@RequestMapping("view")
	public String view(@RequestParam("no")int no, Model model) {
		Route route = routeService.getRoute(no);
		
		model.addAttribute("route", route);
		return "route/view";
	}	
	
	/// viewforajax
	@RequestMapping(value="view", produces="application/json")
	@ResponseBody
	public Route viewForAjax(@RequestParam("routeNo")int no, Model model) {
		return routeService.getRoute(no);
	}
	
	
	/// form
	@RequestMapping(value="form", method=RequestMethod.GET)
	public String form() {
		return "route/form";
	}
	
	/// delete
		@RequestMapping("delete")
		protected String delete(@RequestParam("no") int no) throws Exception {
			routeService.removeRoute(no);
			return "route/delete";
		}
		
		/// deleteforjson
		@RequestMapping(value="delete", produces="application/json")
		@ResponseBody
		protected Map<String, Object> deleteForJson(@RequestParam("no") int no) throws Exception {
			routeService.removeRoute(no);
			
			HashMap<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("status", "success");
			resultMap.put("no", Integer.toString(no));
			return resultMap; 
		}
	
	
	
	/// add
	@RequestMapping(value="form", method=RequestMethod.POST)
	public String add(
			@ModelAttribute("route") Route route,
			BindingResult result, 
			SessionStatus status) throws Exception {
		
		//this.addPhoto(route, route.getPhoto1());
		//this.addPhoto(route, route.getPhoto2());
		//this.addPhoto(route, route.getPhoto3());
		
		routeService.addRoute(route);
		
		return "route/add";
	}
	
	
	/// addforajax
	@RequestMapping(value="add", method=RequestMethod.POST)
	@ResponseBody
	public String addForAjax(
			@ModelAttribute("route") Route route,
			BindingResult result, 
			SessionStatus status) throws Exception {
		//this.addPhoto(route, route.getPhoto1());
		//this.addPhoto(route, route.getPhoto2());
		//this.addPhoto(route, route.getPhoto3());
		
		routeService.addRoute(route);
		
		Route newRoute = new Route();
		newRoute.setRouteNo(route.getRouteNo());
		newRoute.setName(route.getName());
		newRoute.setStartPointLat(route.getStartPointLat());
		newRoute.setStartPointLngt(route.getStartPointLngt());
		newRoute.setEndPointLat(route.getEndPointLat());
		newRoute.setEndPointLngt(route.getEndPointLngt());
		
		// fromObject 媛앹껜濡쒕���json �ㅻ툕�앺듃瑜�戮묒븘��
		// json-lib 2.4 (classfier jdk1.5)
		return JSONObject.fromObject(newRoute).toString();
	}
	
	
	/*
	///updateform
	@RequestMapping(value="updateForm", method=RequestMethod.GET)
	public String updateForm(@RequestParam("no") int no, Model model) {
		model.addAttribute("route", routeService.getRoute(no));
		return "route/updateForm";
	}
	*/
	
	/*
	/// update
	@RequestMapping(value="updateForm", method=RequestMethod.POST)
	public String update(
			@ModelAttribute("route") Route route,
			BindingResult result, 
			SessionStatus status) throws Exception {
		
		this.addPhoto(route, route.getPhoto1(), route.getPhoto1Origin());
		this.addPhoto(route, route.getPhoto2(), route.getPhoto2Origin());
		this.addPhoto(route, route.getPhoto3(), route.getPhoto3Origin());
		
		routeService.modifyRoute(route);
		
		return "route/update";
	}
	*/
	
	/*
	/// updateforajax
	@RequestMapping(
			value="update", 
			method=RequestMethod.POST)
	@ResponseBody
	public String updateForAjax(
			@ModelAttribute("route") Route route,
			BindingResult result, 
			SessionStatus status) throws Exception {
		
		this.addPhoto(route, route.getPhoto1(), route.getPhoto1Origin());
		this.addPhoto(route, route.getPhoto2(), route.getPhoto2Origin());
		this.addPhoto(route, route.getPhoto3(), route.getPhoto3Origin());
		
		routeService.modifyRoute(route);
		
		Route newRoute = new Route();
		newRoute.setRouteNo(route.getRouteNo());
		newRoute.setName(route.getName());
		newRoute.setTel(route.getTel());
		newRoute.setAddr(route.getAddr());
		
		return JSONObject.fromObject(newRoute).toString();
	}
	*/
	
	/*
	/// addphoto
	private void addPhoto(Route route, MultipartFile file, String originalFileName) throws Exception {
		if ( !addPhoto(route, file) && !originalFileName.equals("")) {
			route.addPhoto(new Photo(originalFileName));
		}
	}
	*/
		
	/*
	/// addphoto
	private boolean addPhoto(Route route, MultipartFile file) throws Exception {
		String repositoryPath = this.servletContext
				.getRealPath("/upload") + "/";
		
		if (file != null && file.getSize() > 0) {
			route.addPhoto(
					new Photo(
						FileUpDownUtils.saveFile(file, repositoryPath)
					)
			);
			return true;
		}
		return false;
	}
	*/

}
